#include "restoretargetdialog.h"
#include <QGridLayout>
#include <QRadioButton>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>

RestoreTargetDialog::RestoreTargetDialog(QSharedPointer<RestoreTask> task, QWidget *parent) :
    QDialog(parent)
{
    this->restoreTask = task;

    QGridLayout *centralLayout = new QGridLayout();
    this->setLayout(centralLayout);

    QLabel *text = new QLabel(tr("Where would you like to place restored files?"));
    centralLayout->addWidget(text, 0, 0, 1, 2);

    originalPathRButton = new QRadioButton(tr("Use original path"));
    centralLayout->addWidget(originalPathRButton, 1, 0, 1, 2);

    newPathRButton = new QRadioButton(tr("Use different path"));
    centralLayout->addWidget(newPathRButton, 2, 0, 1, 2);

    newPathValue = new QLineEdit();
    centralLayout->addWidget(newPathValue, 3, 0);

    QPushButton *newPathSelectButton = new QPushButton(tr("..."));
    centralLayout->addWidget(newPathSelectButton, 3, 1);

    QHBoxLayout *buttonLayout = new QHBoxLayout();
    centralLayout->addLayout(buttonLayout, 4, 0, 1, 2);

    QPushButton *okButton = new QPushButton(tr("Ok"));
    connect(okButton, &QPushButton::clicked, this, &RestoreTargetDialog::onOKButtonClicked);
    buttonLayout->addWidget(okButton);

    QPushButton *cancelButton = new QPushButton(tr("Cancel"));
    connect(cancelButton, &QPushButton::clicked, this, &RestoreTargetDialog::onCancelButtonClicked);
    buttonLayout->addWidget(cancelButton);
}

void RestoreTargetDialog::onOKButtonClicked()
{
    if(originalPathRButton->isChecked()) {
        this->restoreTask->setLocation(RestoreTask::Original);
        this->restoreTask->setTargetPath(restoreTask->getSourcePath());
    } else {
        this->restoreTask->setLocation(RestoreTask::Different);
        this->restoreTask->setTargetPath(newPathValue->text());
    }
    this->accept();
}

void RestoreTargetDialog::onCancelButtonClicked()
{
    this->reject();
}
